﻿/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Qurihara.Anm
{
    public class AnmButton : Button, IAnmElement
    {

        public Rectangle BoundingBox
        {
            get { return base.Bounds; }
            set { this.Bounds = value; }
        }
        //			void Invalidate();
        private IAnm anm;
        public IAnm Anm
        {
            get { return anm; }
            set { anm = value; }
        }
    }

    public class AnmButton2 : IAnmElement
    {
        public AnmButton2()
        {
            button = new System.Windows.Forms.Button();
        }
        protected System.Windows.Forms.Button button;
        public System.Windows.Forms.Button Button
        {
            get { return button; }
            set { button = value; }
        }
        public Rectangle BoundingBox
        {
            get { return button.Bounds; }
            set { button.Bounds = value; }
        }
        public bool Visible
        {
            get { return button.Visible; }
            set { button.Visible = value; }
        }
        public void Invalidate()
        {
            // do nothing because button has autoredraw
        }
        public void Invalidate(Rectangle r)
        {
            // do nothing because button has autoredraw
        }
        private IAnm anm;
        public IAnm Anm
        {
            get { return anm; }
            set { anm = value; }
        }
    }
    /*
        public class ButtonAnmer
        {
            public AnmButton2 anmButton;
            public ButtonAnmer()
            {
                anmButton = new AnmButton2();
                anmButton.Button.Click += new EventHandler(this.Click);
                anmButton.Button.MouseEnter += new EventHandler(this.Enter);
                anmButton.Button.MouseLeave += new EventHandler(this.Leave);
            }
            public ButtonAnmer(int jumpD,Rectangle initR,Rectangle destR,int dura,int intvl,RectMoveAnm.AnmType at): this()
            {
                SetActionParam(jumpD,initR,destR,dura,intvl,at);
            }

            protected int jumpDist;
            protected Rectangle initRectangle,destRectangle;
            protected int duration,interval;
            protected RectMoveAnm.AnmType anmType;
            public void SetActionParam(int jump,Rectangle initR,Rectangle destR,int dura,int intvl,RectMoveAnm.AnmType at)
            {
                jumpDist = jump;
                initRectangle = initR;
                destRectangle = destR;
                duration = dura;
                interval = intvl;
                anmType = at;
            }
            protected IAnm anmActive;
            protected enum State
            {
                Jump,
                Expand,
                Shrink
            }
            protected State state;
            public void Click(object o,EventArgs e)
            {
                if (anmActive != null) anmActive.Stop();
                if (anmActive == null || anmActive.IsActive == false)
                {
                    state = State.Jump;
                    anmActive = new JumpAnm(anmButton,jumpDist,duration,interval);
                    anmActive.Start();
                }

            }

            //int c= 0;
            //int d= 0;
            public void Enter(object o,EventArgs e)
            {
                // See how many times this event invokes
                //anmButton.Text = c.ToString() + " " + d.ToString();
                //c++;
                if (anmActive == null || anmActive.IsActive == false)
                {
                    state = State.Expand;
                    anmActive = new RectMoveAnm(anmButton,initRectangle,destRectangle,duration,interval,anmType);
                    anmActive.Start();
                }
                if (anmActive.IsActive == true && state == State.Shrink)
                {
                    anmActive.AnmFinishedHandler += new EventHandler(this.Enter);
                }

            }
            public void Leave(object o,EventArgs e)
            {
                //d++;
                if (anmActive == null || anmActive.IsActive == false)
                {
                    state = State.Shrink;
                    anmActive = new RectMoveAnm(anmButton,destRectangle,initRectangle,duration,interval,anmType);
                    anmActive.Start();
                }
                if (anmActive.IsActive == true && state == State.Expand)
                {
                    anmActive.AnmFinishedHandler += new EventHandler(this.Leave);
                }
            }

        }
    */
    public class ButtonAnmer : IAnmElement
    {
        public event EventHandler Click;
        public ButtonAnmer()
        {
            button = new System.Windows.Forms.Button();
            button.Click += new EventHandler(this.InvokeAnm);
            //button.MouseDown += new MouseEventHandler(this.Click);
            button.MouseEnter += new EventHandler(this.Enter);
            button.MouseLeave += new EventHandler(this.Leave);
        }
        public ButtonAnmer(int jumpD, Rectangle initR, Rectangle destR, int dura, RectMoveAnm.AnmType at)
            : this()
        {
            SetActionParam(jumpD, initR, destR, dura, at);
        }

        protected System.Windows.Forms.Button button;
        public System.Windows.Forms.Button Button
        {
            get { return button; }
            set { button = value; }
        }
        public Rectangle BoundingBox
        {
            get { return button.Bounds; }
            set { button.Bounds = value; }
        }
        public bool Visible
        {
            get { return button.Visible; }
            set { button.Visible = value; }
        }
        public void Invalidate()
        {
            // do nothing because button has autoredraw
        }
        public void Invalidate(Rectangle r)
        {
            // do nothing because button has autoredraw
        }
        protected IAnm anm;
        public IAnm Anm
        {
            get { return anm; }
            set { anm = value; }
        }

        protected int jumpDist;
        protected Rectangle initRectangle, destRectangle;
        protected int duration;//,interval;
        protected RectMoveAnm.AnmType anmType;
        public void SetActionParam(int jump, Rectangle initR, Rectangle destR, int dura, RectMoveAnm.AnmType at)
        {
            jumpDist = jump;
            initRectangle = initR;
            destRectangle = destR;
            duration = dura;
            //interval = intvl;
            anmType = at;
        }
        protected IAnm anmActive;
        protected enum State
        {
            Jump,
            Expand,
            Shrink
        }
        protected State state;
        private void InvokeAnm(object o, EventArgs e)
        //public void Click(object o,MouseEventArgs e)
        {
            if (anmActive != null && anmActive.IsActive) anmActive.Finish();
            if (anmActive == null || anmActive.IsActive == false)
            {
                state = State.Jump;
                anmActive = new JumpAnm(this, jumpDist, duration);
                anmActive.AnmFinishedHandler += new EventHandler(this.Fire);
                anmActive.AnmFinishedHandler += new EventHandler(this.Leave); //
                anmActive.Start();
            }
        }
        protected void Fire(object o, EventArgs e)
        {
            if (Click != null) Click(this, new EventArgs());
        }

        //int c= 0;
        //int d= 0;
        public void Enter(object o, EventArgs e)
        {
            // See how many times this event invokes
            //anmButton.Text = c.ToString() + " " + d.ToString();
            //c++;
            if (anmActive == null || anmActive.IsActive == false)
            {
                state = State.Expand;
                anmActive = new RectMoveAnm(this, initRectangle, destRectangle, duration, anmType);
                anmActive.Start();
            }
            if (anmActive.IsActive == true && state == State.Shrink)
            {
                anmActive.AnmFinishedHandler += new EventHandler(this.Enter);
            }

        }
        public void Leave(object o, EventArgs e)
        {
            //d++;
            if (anmActive == null || anmActive.IsActive == false)
            {
                state = State.Shrink;
                anmActive = new RectMoveAnm(this, destRectangle, initRectangle, duration, anmType);
                anmActive.Start();
            }
            if (anmActive.IsActive == true && state == State.Expand)
            {
                anmActive.AnmFinishedHandler += new EventHandler(this.Leave);
            }
        }

    }

    public class SimpleRect : IAnmElement
    {
        protected bool visible = true;
        protected IAnm anm;
        protected Control control;
        protected Rectangle rect;
        protected Color color;
        public SimpleRect(Control c, Rectangle rct, Color co)
        {
            control = c;
            rect = rct;
            color = co;
        }
        public Rectangle BoundingBox
        {
            get { return rect; }
            set { rect = value; }
        }
        public bool Visible
        {
            get { return visible; }
            set { visible = value; }
        }
        public void Invalidate()
        {
            //control.Invalidate();
            control.Invalidate(this.BoundingBox);
        }
        public void Invalidate(Rectangle r)
        {
            control.Invalidate(r);
        }
        public IAnm Anm
        {
            get { return anm; }
            set { anm = value; }
        }

        public virtual void draw(Graphics g)
        {
            if (visible)
            {
                Brush b = new SolidBrush(color);
                g.FillRectangle(b, rect);
                b.Dispose();
            }
        }
    }

    public class SimpleRect2 : SimpleRect, IPlayBackAnmElement
    {
        public SimpleRect2(Control c, Rectangle rct, Color co)
            : base(c, rct, co)
        {
        }
        public override void draw(Graphics g)
        {
            Brush b = new SolidBrush(Color.FromArgb((int)(255 * playPosition), color));
            g.FillRectangle(b, rect);
            b.Dispose();
        }
        protected float playPosition;
        public float PlayPosition
        {
            get { return playPosition; }
            set { playPosition = value; }
        }
    }

}
